package com.witte.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.witte.entity.*;
import com.witte.service.CusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/api/Customer")
@CrossOrigin
public class CusController {

    @Autowired
    private CusService cusService;

    @GetMapping(value = "/findAllCus")
    public RespBean begin() {
        RespBean respBean = RespBean.ok(null, cusService.selectAllCus());
        return respBean ;
    }

    @GetMapping(value = "/findAllCusByLevel")
    public RespBean selectLevel() {
    	RespBean respBean = RespBean.ok(null, cusService.selectAllCusByLevel());
		return respBean;
    }

    @GetMapping(value = "/findAllCusByStatisfy")
    public RespBean selectStatisfy() {
    	RespBean respBean = RespBean.ok(null, cusService.selectAllCusByStatify());
		return respBean;
    }

    @PostMapping(value = "/insertInfo")
    public RespBean insert(@RequestBody Customer customer) {
        RespBean insertCusInfo =RespBean.ok(null, cusService.insert(customer));
        return insertCusInfo;
    }

    @GetMapping(value = "/getStateList")
    public RespBean getStateList(Integer beginPage,Integer pageSize){
        List<Customer> list = cusService.getStateList(beginPage,pageSize);
        RespBean respBean = RespBean.error("新加错误！");
        if(list!=null) {
            respBean = RespBean.ok("ok",list);
            respBean.setTotleSize(cusService.getRecodeCount());
        }
        return respBean;
    }

    @PostMapping(value = "/updateInfo")
    public RespBean update(@RequestBody Customer customer) {
        RespBean respBean = RespBean.ok(null, cusService.updateByCusNo(customer));
        return respBean;
    }

    @PostMapping(value = "/deleteInfo")
    public RespBean delete(@RequestBody Customer customer) {
        RespBean respBean =RespBean.ok(null, cusService.deleteByPrimaryKey(customer.getNo()));
        return respBean ;
    }

    @GetMapping(value = "/muchDel")
    public RespBean deleteNos(String nos) {
        String[] cusNos= nos.split(",");
        RespBean respBean = null;
//        RespBean respBean =RespBean.ok(null, cusService.deleteByPrimaryKey(cusNos.getNo()));
        for(int i=0; i<cusNos.length; i++) {
            respBean = RespBean.ok(null, cusService.deleteByPrimaryKey(Integer.parseInt(cusNos[i])));
        }
        return respBean;
    }

    @GetMapping("/searchName")
    public RespBean getNameList(String name,Integer beginPage,Integer pageSize){

        List<Customer> list=cusService.getNameList(name,beginPage,pageSize);
        RespBean respBean=RespBean.error("获取错误！");
        if(list!=null) {
            respBean=RespBean.ok("ok",list);
            respBean.setTotleSize(cusService.getNameCount(name));
        }
        //System.out.println(beginPage + ',' + pageSize);
       /// System.out.println(cusService.getRecodeCount());
        return respBean;
    }
}
